package com.zxl.app.controller.member;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zxl.app.context.UserContext;
import com.zxl.app.service.MemberService;
import com.zxl.common.Result;
import com.zxl.common.mapper.SysMemberAddressMapper;
import com.zxl.common.model.bo.meber.MemberUpdateBo;
import com.zxl.common.model.bo.meber.UpdateAddrBo;
import com.zxl.common.model.pojo.SysMemberAddress;
import com.zxl.common.model.vo.member.MemberInfoDetailVo;
import com.zxl.common.model.vo.member.ShoppingAddrListVo;
import com.zxl.common.service.SysMemberAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jacoco.agent.rt.internal_43f5073.core.internal.flow.IFrame;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zxw
 * @date 2023/4/1
 */
@RestController
@RequestMapping("/app/member")
@Api(tags = {"会员模块"})
public class MemberController {
    @Resource
    private MemberService memberService;
    @Resource
    private SysMemberAddressService sysMemberAddressService;
    @Resource
    private SysMemberAddressMapper sysMemberAddressMapper;


    @ApiOperation("个人中心")
    @GetMapping("/info")
    public Result<MemberInfoDetailVo> memberInfo(){
        MemberInfoDetailVo memberInfo = memberService.memberInfo(UserContext.getUserId());
        return new Result<>(memberInfo);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/update")
    public Result memberUpdate(@RequestBody MemberUpdateBo memberUpdateBo){
        memberUpdateBo.setMemberId(UserContext.getUserId());
        memberService.memberUpdate(memberUpdateBo);
        return Result.success();
    }

    @ApiOperation("收货地址列表")
    @GetMapping("/address/list")
    public Result<List<ShoppingAddrListVo>> list() {
        List<ShoppingAddrListVo> shoppingAddrListVos = sysMemberAddressService.listAddr(UserContext.getUserId());
        return new Result(shoppingAddrListVos);
    }



    @ApiOperation("修改收货地址信息")
    @PostMapping("/address/update")
    public Result update(@RequestBody @Valid UpdateAddrBo bo) {
        SysMemberAddress sysMemberAddress = new SysMemberAddress();
        BeanUtil.copyProperties(bo,sysMemberAddress);
        if (bo.getDefaultStatus()){
            sysMemberAddressMapper.update(null, new UpdateWrapper<SysMemberAddress>()
                    .eq("member_id", UserContext.getUserId())
                    .set("default_status", 0));
        }
        sysMemberAddress.setMemberId(UserContext.getUserId());
        sysMemberAddressService.updateById(sysMemberAddress);
        return Result.success();
    }

    @ApiOperation("添加收货地址信息")
    @PostMapping("/address/add")
    public Result add(@RequestBody @Valid UpdateAddrBo bo) {
        SysMemberAddress sysMemberAddress = new SysMemberAddress();
        BeanUtil.copyProperties(bo,sysMemberAddress);
        sysMemberAddress.setMemberId(UserContext.getUserId());
        Boolean defaultStatus = bo.getDefaultStatus();
        if (defaultStatus){
            sysMemberAddress.setDefaultStatus(1);
        }else {
            sysMemberAddress.setDefaultStatus(0);
        }
        sysMemberAddressService.save(sysMemberAddress);
        return Result.success();
    }

    @ApiOperation("删除收货地址信息")
    @GetMapping("/address/del")
    public Result addressDel(@ApiParam(value = "地址ID",required = true)@RequestParam("id")Integer id){
        sysMemberAddressMapper.deleteById(id);
        return Result.success();
    }
}
